package com.suneast.suneaststore.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.app.ConfigKeys;
import com.suneast.suneaststore.app.Latte;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat_pay_result);
        initView();
        api = WXAPIFactory.createWXAPI(this, Latte.getConfiguration(ConfigKeys.WE_CHAT_APP_ID));
        api.handleIntent(getIntent(), this);
    }

    private RelativeLayout paySuccessLayout;
    private RelativeLayout payCanceledLayout;
    private RelativeLayout payFailedLayout;

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener((View v) -> {
            finish();
        });
        paySuccessLayout = findViewById(R.id.layout_pay_success);
        payCanceledLayout = findViewById(R.id.layout_pay_canceled);
        payFailedLayout = findViewById(R.id.layout_pay_failed);
        Button confirmBtn = findViewById(R.id.btn_confirm);
        confirmBtn.setOnClickListener((View v) -> {
            finish();
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d("tag", "onWeChatPayResp, errCode = " + baseResp.errCode);
        if (baseResp.errCode == 0) {
            Log.e(TAG, "onResp: baseResp.errCode == 0");
            paySuccessLayout.setVisibility(View.VISIBLE);
            payCanceledLayout.setVisibility(View.GONE);
            payFailedLayout.setVisibility(View.GONE);
            Toast.makeText(this, "支付成功", Toast.LENGTH_LONG).show();
            //todo 去后台请求支付结果
        } else if (baseResp.errCode == -2) {
            Log.e(TAG, "onResp: baseResp.errCode == -2");

            paySuccessLayout.setVisibility(View.GONE);
            payCanceledLayout.setVisibility(View.VISIBLE);
            payFailedLayout.setVisibility(View.GONE);
        } else if (baseResp.errCode == -1) {
            Log.e(TAG, "onResp: baseResp.errCode == -1");
            paySuccessLayout.setVisibility(View.GONE);
            payCanceledLayout.setVisibility(View.GONE);
            payFailedLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }
}